package com.zhiyume.controller;

import com.zhiyume.dao.entity.TDrama;
import com.zhiyume.dao.entity.TDramaWithBLOBs;
import com.zhiyume.service.DramaActorService;
import com.zhiyume.service.IDramaService;
import com.zhiyume.service.util.DateUtil;
import com.zhiyume.util.TypeConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 剧增删改查
 */
@Controller
@RequestMapping("/drama")
public class DramaController {

    @Autowired
    private IDramaService iDramaService;
    @Autowired
    private DramaActorService dramaActorService;

    /**
     * 返回剧库添加页面
     * @param model
     * @return
     */
    @RequestMapping("/addDramaPage")
    public ModelAndView addDramaPage(Model model){

        ModelAndView modelAndView = new ModelAndView();
        TDrama drama = new TDrama();

        List dramaTypeList = Arrays.asList(TypeConstants.dramaTypeArr);

        drama.setFirstPlayTime(DateUtil.shortDate(new Date()));

        model.addAttribute("dramaTypeList",dramaTypeList);
        model.addAttribute("drama",drama);
        modelAndView.setViewName("page/drama/dramaAdd");
        return modelAndView;
    }


    /**
     * 剧库保存
     * @param drama
     * @return
     */
    @RequestMapping("/addDrama")
    @ResponseBody
    public Map addDrama(TDramaWithBLOBs drama){
        String result = "success";
        try {
            iDramaService.saveDrama(drama);
            dramaActorService.addDramaPerson(drama);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }

        Map map = new HashMap();
        map.put("result",result);

        return map;
    }

    /**
     * 返回剧库列表页面
     * @param model
     * @return
     */
    @RequestMapping("/dramaListPage")
    public ModelAndView dramaList(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/drama/dramaList");
        return modelAndView;
    }

    /**
     * 获取剧库列表
     * @param page
     * @param pageSize
     * @param title   剧库标题
     * @param name    剧名称
     * @param startTime
     * @param endTime
     * @return
     */
    @RequestMapping(value="/getDramaList")
    @ResponseBody
    public Object getInformationList(@RequestParam(defaultValue = "0") int page,
                                     @RequestParam(defaultValue = "10") int pageSize,
                                     @RequestParam(defaultValue = "") String title,
                                     @RequestParam(defaultValue = "") String name,
                                     @RequestParam(defaultValue = "") String startTime,
                                     @RequestParam(defaultValue = "") String endTime){
        Map dataMap = iDramaService.findDramaList(page,pageSize,title,name,startTime,endTime);
        return dataMap;
    }


    /**
     * 返回剧库编辑页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/editDramaPage")
    public ModelAndView editDramaPage(Model model,@RequestParam("id") long id){
        TDramaWithBLOBs tDrama = iDramaService.findDramaById(id);

        // 剧题材下拉选择
        List dramaTypeList = Arrays.asList(TypeConstants.dramaTypeArr);

        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("tDrama",tDrama);
        model.addAttribute("dramaTypeList",dramaTypeList);
        modelAndView.setViewName("page/drama/dramaEdit");
        return modelAndView;
    }

    /**
     * 返回剧库详情页面
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/dramaDetail")
    public ModelAndView dramaDetail(Model model,@RequestParam("id") long id){
        TDramaWithBLOBs tDrama = iDramaService.findDramaById(id);

        // 剧题材下拉选择
        List dramaTypeList = Arrays.asList(TypeConstants.dramaTypeArr);

        ModelAndView modelAndView = new ModelAndView();
        model.addAttribute("tDrama",tDrama);
        model.addAttribute("dramaTypeList",dramaTypeList);
        modelAndView.setViewName("page/drama/dramaDetail");
        return modelAndView;
    }

    /**
     * 删除剧库
     */
    @RequestMapping("/deleteDramaById")
    @ResponseBody
    public Map deleteDramaById(Model model,@RequestParam("id") long id){
        String result = "success";
        try {
            iDramaService.deleteDramaById(id);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }


    /**
     * 剧库更新
     */
    @RequestMapping("updateDrama")
    @ResponseBody
    public Map updateDrama(@ModelAttribute("form") TDramaWithBLOBs tDrama){
        String result = "success";
        try {
            iDramaService.updateDrama(tDrama);
            dramaActorService.addDramaPerson(tDrama);
        }catch (Exception e){
            result = "fail";
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("result",result);
        return map;
    }

    /**
     * 剧库审核页面
     */
    @RequestMapping("/dramaCheckList")
    public ModelAndView dramaCheckList(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/drama/dramaCheckList");
        return modelAndView;
    }

    /**
     * 剧库审核状态更新
     */
    @RequestMapping("updateDramaStatus")
    @ResponseBody
    public Map updateDramaStatus(@RequestParam("") long id,
                                 @RequestParam("") String status){
        String result = "success";
        Map map = new HashMap();
        try {
            iDramaService.updateDramaStatus(id,status);
        }catch (Exception e){
            result = "";
            e.printStackTrace();
        }
        map.put("result",result);
        return map;
    }
}
